package com.api.gateway.service.impl;

import com.api.gateway.service.DecryptService;
import com.api.gateway.util.AESUtils;
import com.api.gateway.util.ConstUtils;
import com.api.gateway.util.RSAUtils;
import org.springframework.stereotype.Service;

import java.security.NoSuchAlgorithmException;
import java.security.spec.InvalidKeySpecException;

/**
 * Created by hao.g on 18/4/2.
 */
@Service
public class DecryptServiceImpl implements DecryptService {

    @Override
    public String decryptSign(String param) throws InvalidKeySpecException, NoSuchAlgorithmException {
        return RSAUtils.publicDecrypt(param, RSAUtils.getPublicKey(ConstUtils.CLIENT_PUBLIC_KEY));
    }

    @Override
    public boolean validateSign(String src, String desc) {
        return src.equals(desc);
    }

    @Override
    public String decryptData(String data, String signClear) throws InvalidKeySpecException, NoSuchAlgorithmException {
        String aesKeyClear = RSAUtils.privateDecrypt(signClear, RSAUtils.getPrivateKey(ConstUtils.SERVER_PRIVATE_KEY));
        return AESUtils.decrypt(data, aesKeyClear);
    }
}